package com.debao;

import com.debao.common.annotation.Anonymous;
import com.debao.common.core.domain.AjaxResult;
import com.debao.common.utils.SecurityUtils;
import com.debao.system.domain.HarvestAddress;
import com.debao.system.service.IHarvestAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 收货地址
 */
@RestController
@RequestMapping("/api/address")
@CrossOrigin
public class ApiAddress {

    @Autowired
    private IHarvestAddressService addressService;

    /**
     * 我的收货地址列表
     * @return
     */
    @GetMapping("/list")
    public AjaxResult list() {
        Long userId = SecurityUtils.getUserId();
        return AjaxResult.success( addressService.selectHarvestAddressList(new HarvestAddress().setUserId(userId)));
    }

    /**
     * 我的收货地址详情
     * @param id
     * @return
     */
    @GetMapping("info/{id}")
    @Anonymous
    public AjaxResult info(@PathVariable("id") Long id) {
        return AjaxResult.success(addressService.selectHarvestAddressById(id));
    }

    /**
     * 新增收货地址
     * @param address
     * @return
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody @Valid HarvestAddress address) {
        address.setUserId(SecurityUtils.getUserId());
        return AjaxResult.success(addressService.insertHarvestAddress(address));
    }
    /**
     * 修改收货地址
     * @param address
     * @return
     */
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody @Valid HarvestAddress address) {
        return AjaxResult.success(addressService.updateHarvestAddress(address));
    }
   /**
     * 删除收货地址
     * @param id
     * @return
     */
    @DeleteMapping("/remove/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return AjaxResult.success(addressService.deleteHarvestAddressById(id));
    }
}
